/**
 * 2013 2013-5-9
 * @author daslkghsdkjgh
 */
package com.surfilter.dp.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;

import com.surfilter.dp.context.ContextHolder;
import com.surfilter.dp.util.PropertyFileManager;
import com.surfilter.dp.util.ViewUtil;

/**
 * @author daslkghsdkjgh
 */
public class ConfigDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField_url;
	private JTextField textField_name;
	private JTextField textField_pass;
	private JTextField textField_path;
	private JButton okButton;
	private JButton cancelButton;

	/**
	 * Create the dialog.
	 */
	public ConfigDialog(JFrame parent) {
		super(parent);
		setTitle("系统参数配置");
		initView();
		initData();
		initListeners();
	}
	
	/**
	 * 
	 */
	private void initListeners() {
		final ConfigDialog dialog = this;
		okButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				PropertyFileManager.setProp("jdbcUrl", textField_url.getText());
				PropertyFileManager.setProp("user", textField_name.getText());
				PropertyFileManager.setProp("password", textField_pass.getText());
				PropertyFileManager.setProp("project_path", textField_path.getText());
				PropertyFileManager.save();
				dialog.dispose();
				JOptionPane.showMessageDialog(ContextHolder.getInstance().getMainWin(), "保存成功");
			}
		});
		cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
	}

	/**
	 * 
	 */
	private void initData() {
		textField_url.setText(PropertyFileManager.getProp("jdbcUrl"));
		textField_name.setText(PropertyFileManager.getProp("user"));
		textField_pass.setText(PropertyFileManager.getProp("password"));
		textField_path.setText(PropertyFileManager.getProp("project_path"));
	}

	private void initView(){
		setModal(true);
		setBounds(100, 100, 450, 271);
		ViewUtil.toCenter(this);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("数据库连接串");
			lblNewLabel.setBounds(22, 32, 95, 15);
			contentPanel.add(lblNewLabel);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("数据库用户");
			lblNewLabel_1.setBounds(22, 73, 95, 15);
			contentPanel.add(lblNewLabel_1);
		}
		{
			JLabel lblNewLabel_2 = new JLabel("数据库密码");
			lblNewLabel_2.setBounds(22, 116, 95, 15);
			contentPanel.add(lblNewLabel_2);
		}
		{
			JLabel lblNewLabel_3 = new JLabel("WebOS项目路径");
			lblNewLabel_3.setBounds(22, 158, 95, 15);
			contentPanel.add(lblNewLabel_3);
		}
		{
			textField_url = new JTextField();
			textField_url.setBounds(127, 29, 283, 18);
			contentPanel.add(textField_url);
			textField_url.setColumns(10);
		}
		{
			textField_name = new JTextField();
			textField_name.setColumns(10);
			textField_name.setBounds(127, 70, 283, 18);
			contentPanel.add(textField_name);
		}
		{
			textField_pass = new JTextField();
			textField_pass.setColumns(10);
			textField_pass.setBounds(127, 113, 283, 18);
			contentPanel.add(textField_pass);
		}
		{
			textField_path = new JTextField();
			textField_path.setColumns(10);
			textField_path.setBounds(127, 156, 283, 18);
			contentPanel.add(textField_path);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("保存");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("取消");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

}
